﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLog;

namespace FileSharingHosting
{
    public partial class File : System.Web.UI.Page
    {
        static readonly Logger _log = LogManager.GetCurrentClassLogger();

        protected void Page_Load(object sender, EventArgs e)
        {
            _log.Debug("File download page requested.");
            if (Request.RequestType == "GET")
            {
                if (Request.Params["link"] != null)
                {
                    string link = Request.Params["link"];
                    _log.Debug("Loading file info from db.");
                    var fbll = new Business.FileBusinessLogic();
                    var requested_file = fbll.LoadHostedFile(fbll.GetFileID(link));
                    
                    if (requested_file.ID == -1)
                    {
                        _log.Debug("File not found");
                        Table1.Visible = false;
                        lbl_error.Visible = true;
                    }
                    else
                    {
                        _log.Debug("Loaded file... showing download link");
                        filedownload.NavigateUrl = string.Format("FileHandler.ashx?id={0}", link);
                    }
                }
            }
        }
    }
}